package tempdao;

import java.util.Calendar;
import java.util.List;

import javax.naming.InitialContext;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dao.*;
import util.*;

public class EgnPositionDao {
	private final static Logger logger = LoggerFactory.getLogger(EgnPositionDao.class);

	public static List<EgnMarketVari> getAllEgnMarketVari() {
		List<EgnMarketVari> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnMarketVari instance");
		Query qr = s.createQuery("from EgnMarketVari" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnMarketVari> getTypedEgnMarketVari(String mvType) {
		List<EgnMarketVari> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get Typed EgnMarketVari instance");
		Query qr = s.createQuery("from EgnMarketVari a where a.mvType =:param" );
		qr.setParameter("param", mvType);

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<String> getEgnMarketVariTypes() {
		List<String> rst ;
		logger.debug("Get EgnMarketVari Types");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery("select distinct a.mvType from EgnMarketVari a" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static Calendar getEgnMarketVairHisLastDate(){
		Calendar rst;
		logger.debug("getEgnMarketVairHisLastDate");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery(" select max(a.id.baseDate) from EgnMarketVariHis a ");
		try {
			rst = (Calendar)qr.uniqueResult();
//			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}

	}
	public static Calendar getEgnMarketVairHisLastDate(String mvId){
		Calendar rst;
		logger.debug("getEgnMarketVairHisLastDate");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery(" select max(a.id.baseDate) from EgnMarketVariHis a " +
								 " where a.id.mvId =:param" );
		qr.setParameter("param", mvId);
		try {
			rst = (Calendar)qr.uniqueResult();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}

	}


	public static List<EgnMarketVariHis> getEgnMarketVariHis(Calendar cal, int cnt) {
		List<EgnMarketVariHis> rst ;
		logger.debug("Get EgnMarketVariHis");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery("from EgnMarketVariHis a where a.id.baseDate =:param" );
		qr.setParameter("param", cal);
		qr.setMaxResults(cnt);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnMarketVariHis> getEgnMarketVariHis(Calendar cal) {
		List<EgnMarketVariHis> rst ;
		logger.debug("Get EgnMarketVariHis");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery("from EgnMarketVariHis a where a.id.baseDate =:param" );
		qr.setParameter("param", cal);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

	public static List<EgnMarketVariHis> getEgnMarketVariHis(String mvId) {
		List<EgnMarketVariHis> rst ;
		logger.debug("Get EgnMarketVariHis");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery("from EgnMarketVariHis a where a.id.mvId =:param" );
		qr.setParameter("param", mvId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnMarketVariHis> getEgnMarketVariHis(String mvId,Calendar cal) {
		List<EgnMarketVariHis> rst ;
		logger.debug("Get EgnMarketVariHis");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery(" from EgnMarketVariHis a " +
								 " where a.id.mvId =:param " +
								 " and a.id.baseDate = :param2 "
								 );
		qr.setParameter("param", mvId);
		qr.setParameter("param2", cal);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

}
